package com.supreme.studyserver.config;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.web.context.annotation.RequestScope;
import org.springframework.web.context.annotation.SessionScope;

@Configuration
public class ScopeConfig implements InitializingBean, DisposableBean {

    @Bean(value = "singletonScope")
    @Scope(value = BeanDefinition.SCOPE_SINGLETON)
    public JSONObject singletonScope() {
        return new JSONObject().fluentPut("title", "singletonScope");
    }

    @Bean(value = "prototypeScope")
    @Scope(value = BeanDefinition.SCOPE_PROTOTYPE)
    public JSONObject prototypeScope() {
        return new JSONObject().fluentPut("title", "prototypeScope");
    }

    @Bean(value = "requestScope")
    @RequestScope
    public JSONObject requestScope() {
        return new JSONObject().fluentPut("title", "requestScope");
    }

    @Bean(value = "sessionScope")
    @SessionScope
    public JSONObject sessionScope() {
        return new JSONObject().fluentPut("title", "sessionScope");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("DisposableBean destroy");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("InitializingBean afterPropertiesSet");
    }
}
